﻿package Classes.GameUtils
{	
	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.display.Stage;
	import flash.geom.Point;
	import flash.geom.Rectangle;
	import flash.text.*;
	
	/**
	 * 提示显示原件类 
	 * @author 翼翔天外
	 * 
	 */
	internal class Tip extends Sprite
	{
		/**
		 * 提示的显示对象类 
		 */
		private var _tipSprite:BaseTipSprite;		
		/**
		 * 皮肤 
		 */
		private var _skin:Sprite;
		/**
		 * 背景 
		 */
		private var _bg:Sprite;
		/**
		 * 箭头 
		 */
		private var _arrow:Sprite;
		
		private var _offx:int;
		
		private var _offy:int;
	
		//显示方向 
		private var _direction:int;	
		//目标的矩形范围
		private var _targetBounds:Rectangle;	
		
		public function Tip(tipSprite:BaseTipSprite)
		{
			super();
			this.mouseEnabled = this.mouseChildren = false;
			_tipSprite = tipSprite;
			init();
		}		

		private function init():void
		{
			_skin = new _tipSprite.skinClass();
			_bg    = _skin.getChildByName("mc_bg") as Sprite;
			_arrow = _skin.getChildByName("mc_arrow") as Sprite;
			
			//皮肤
			addChild(_skin);
			//显示ui
			addChild(_tipSprite);
			_tipSprite.tipBg	=	_skin;
		}
		
		
		/**
		 * 显示方向 
		 */
		public function get direction():int
		{
			return _direction;
		}
		
		/**
		 * @private
		 */
		public function set direction(value:int):void
		{
			_direction = value;
		}
		
		/**
		 * 目标对象矩形区域
		 * @param str 值
		 * 
		 */
		public function set targetBounds(bounds:Rectangle):void
		{
			_targetBounds = bounds;
		}
		
		public function get targetBounds():Rectangle
		{
			return _targetBounds
		}
		
		/**
		 * 获得舞台 
		 * @return 
		 * 
		 */
		private function get myStage():Stage
		{
			return this.stage;
		}
		
		/**
		 * 获得背景笔触宽度 
		 * @return 
		 * 
		 */
		private function get bgBorder():Number
		{
			return _bg.getBounds(_bg).width - _bg.getRect(_bg).width;
		}
		
		/**
		 * 获得自己相对于舞台的矩形区域 
		 * @return 
		 * 
		 */
		private function get myBounds():Rectangle
		{
			return this.getBounds(myStage);
		}
		
		/**
		 * 提示浮动在目标上方 
		 * 
		 */
		private function setTipDirectionUp():void
		{
			//下箭头
			_arrow.x = (_bg.width - bgBorder)/2;
			_arrow.y = _bg.height - bgBorder;//减去笔触宽度，修正位置
			_arrow.rotation = 0;
			
			this.x = targetBounds.x + targetBounds.width/2 - _bg.width/2;
			this.y = this.offy==0? targetBounds.y - _bg.height - _arrow.height:targetBounds.y - _bg.height - _arrow.height+this.offy;
		}
		
		/**
		 * 提示浮动在目标下方
		 * 
		 */
		private function setTipDirectionDown():void
		{
			//上箭头
			_arrow.x = (_bg.width - bgBorder)/2;
			_arrow.y = 0;
			_arrow.rotation = 180;
			
			this.x = targetBounds.x + targetBounds.width/2 - _bg.width/2;
			this.y = this.offy==0? targetBounds.y + targetBounds.height + _arrow.height:targetBounds.y + targetBounds.height + _arrow.height-this.offy;	
		}
		
		/**
		 * 提示浮动在目标左方
		 * 
		 */
		private function setTipDirectionLeft():void
		{
			//右箭头
			_arrow.x = _bg.width - bgBorder;//减去笔触宽度，修正位置
			_arrow.y = (_bg.height - bgBorder)/2;
			_arrow.rotation = 270;
			
			this.x =this.offx==0?  targetBounds.x - _bg.width - _arrow.width:targetBounds.x - _bg.width - _arrow.width+this.offx;
			this.y = targetBounds.y + targetBounds.height/2 - _bg.height/2;					
		}
		
		/**
		 * 提示浮动在目标右方
		 * 
		 */
		private function setTipDirectionRight():void
		{
			//左箭头
			_arrow.x = 0;
			_arrow.y = (_bg.height - bgBorder)/2;	
			_arrow.rotation = 90;
			
			this.x = this.offx==0?targetBounds.x + targetBounds.width + _arrow.width:targetBounds.x + targetBounds.width + _arrow.width-this.offx;
			this.y = targetBounds.y + targetBounds.height/2 - _bg.height/2;	
		}

		/**
		 * 更新位置大小
		 * @param targetBounds 目标的矩形范围
		 * 
		 */
		public function updateSize():void
		{
			//背景大小
			_bg.width 		= _tipSprite.uiWidth + _tipSprite.marginWidth*2;
			_bg.height 		= _tipSprite.uiHeight + _tipSprite.marginHeight*2;
			//背景笔触宽度
			var bgBorder:Number = _bg.getBounds(_bg).width - _bg.getRect(_bg).width;
			//内容ui偏移
			_tipSprite.x 	= _tipSprite.marginWidth;
			_tipSprite.y 	= _tipSprite.marginHeight;
			//限制最小大小
			if(_bg.width < _tipSprite.minWidth) 	_bg.width 	= _tipSprite.minWidth;
			if(_bg.height < _tipSprite.minHeight)	_bg.height  = _tipSprite.minHeight;
			//根据方向显示提示
			switch(direction)
			{
				case ToolTip_Sprite.DIRECTION_UP:
					//---提示浮动在目标上方---
					setTipDirectionUp();
					//auto direction
					if(myBounds.top < 0)
					{
						setTipDirectionDown();
					}
					break;
				
				case ToolTip_Sprite.DIRECTION_DOWN:
					//---提示浮动在目标下方---
					setTipDirectionDown();
					//auto direction
					if(myBounds.bottom > myStage.stageHeight)
					{
						setTipDirectionUp();
					}
					break;
				
				case ToolTip_Sprite.DIRECTION_LEFT:
					//---提示浮动在目标左方---
					setTipDirectionLeft();
					//auto direction
					if(myBounds.left < 0)
					{
						setTipDirectionRight();
					}
					break;
				
				case ToolTip_Sprite.DIRECTION_RIGHT:					
					//---提示浮动在目标右方---
					setTipDirectionRight();
					//auto direction
					if(myBounds.right > myStage.stageWidth)
					{
						setTipDirectionLeft();
					}
					break;
			}
		}

		/**
		 * 偏移量 X  
		 */
		public function get offx():int
		{
			return _offx;
		}

		/**
		 * @private
		 */
		public function set offx(value:int):void
		{
			_offx = value;
		}

		/**
		 * 偏移量 Y 
		 */
		public function get offy():int
		{
			return _offy;
		}

		/**
		 * @private
		 */
		public function set offy(value:int):void
		{
			_offy = value;
		}


	}
}